package swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

import com.sun.demo.ExampleFileFilter;

public class JfilechooserSavedialog {
	public static void main(String[] args) throws ParseException,
			InterruptedException {
//		 test();
		// test1();
//		test2();
	}

	private static void test2() {
		JFrame j = new JFrame();
		j.setSize(300, 200);
		j.setVisible(true);
		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel p = new JPanel();
		j.add(p);
		JButton b = new JButton("save");
		p.add(b);
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser() {
					public String paramString() {
						return "dfsf";
					}
				};

				FileFilter filter = new FileFilter() {

					public boolean accept(File f) {
						return f.isDirectory()
								|| (f.isFile() && (f.getName().endsWith(".htm")
										|| f.getName().endsWith(".HTM")
										|| f.getName().endsWith(".html") || f
										.getName().endsWith(".HTML")));
					}

					public String getDescription() {
						return "保存为HTML文件格式";
					}
				};
				jfc.setFileFilter(filter);
				int i = jfc.showSaveDialog(null); // 打开保存文件对话框

				String fname = null;
				if (i == javax.swing.JFileChooser.APPROVE_OPTION) {
					File f = jfc.getSelectedFile();
					// 注意这里，和下面一句, 如果这里并没有选取中任何的文件，下面的jfc.getName(f)将会返回手输入的文件名
					fname = jfc.getName(f);
					if (fname != null && fname.trim().length() > 0) {
						if (fname.endsWith(".htm") || fname.endsWith(".HTM")
								|| fname.endsWith(".html")
								|| fname.endsWith(".HTML"))
							;
						else {
							fname = fname.concat(".htm");
						}
					}
					if (f.isFile())
						fname = f.getName();
					f = jfc.getCurrentDirectory();
					// 取得要保存的文件的目录,其实getSelectedFile();已经包括了文件路径,这里这样是让大家更易了解

					f = new File(f.getPath().concat(File.separator).concat(
							fname));
					if (f.exists()) {
						i = javax.swing.JOptionPane.showConfirmDialog(null,
								"该文件已经存在，确定要覆盖吗？");
						if (i == javax.swing.JOptionPane.YES_OPTION)
							;
						else
							return;
					}
					try {
						f.createNewFile();
						System.out.println(f);
						java.io.FileWriter fw = new java.io.FileWriter(f);
//						 fw.write(getJtp_html().getText());
						fw.write("123");
						 fw.close();
						// 向要保存的文件写数据
					} catch (Exception ex) {
						javax.swing.JOptionPane.showMessageDialog(null, "出错："
								+ ex.getMessage());
						return;
					}
				}
			}
		});

	}

	private static void test1() {
		JFileChooser chooser = new JFileChooser();
		// Note: source for ExampleFileFilter can be found in FileChooserDemo,
		// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
		ExampleFileFilter filter = new ExampleFileFilter();
		filter.addExtension("jpg ");
		filter.addExtension("gif ");
		filter.setDescription("JPG   &   GIF   Images ");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			System.out.println("You   chose   to   open   this   file:   "
					+ chooser.getSelectedFile().getName());
		}
	}

	private static void test() {
		JFileChooser jFileChooser = new JFileChooser();
		//没有下拉菜单选择
//		File[] files = new File[2];
//		files[0] = new File("123");
//		files[1]= new File("jkl");
//		jFileChooser.setSelectedFiles(files);
		jFileChooser.setSelectedFile(new File("fileToSave.txt"));
		jFileChooser.showSaveDialog(null);
	}
}
